﻿package org.openChart.meta.data
{
	import org.openChart.meta.BaseObject;
	import org.openChart.meta.Style
	
	/**
	 * 
	 * @author Chongyuan
	 */
	public class GroupSet extends BaseObject
	{
		private var groups:Array;
		private var length:uint = 0;
		private var minValue:Number = Number.MAX_VALUE;
		private var maxValue:Number = Number.MIN_VALUE;
		private var totoalValues:Array = new Array();
		
		public function GroupSet( object:Style) 
		{
			super(object);
			groups = new Array();
		}
		
		public function addGroup( group:DataGroup ):void
		{
			this.groups.push(group);
			for (var i:uint = 0 ; i < group.length; i++)
			{
				var element:DataElement = group.getAt(i) ;
				if (totoalValues.length <= i)
				{
					totoalValues.push(element.getValue());
				}
				else
				{
					totoalValues[i] += element.getValue();
				}
			}
			if (minValue > group.minValue )
			{
				minValue = group.minValue;
			}
			if (maxValue < group.maxValue)
			{
				maxValue = group.maxValue;
			}
			this.length++;
		}
		
		public function getGroup( index:uint ):DataGroup
		{
			if (index > 0 && index <= length)
				return groups[index - 1] as DataGroup;
			return null;
		}
		
		public function remove( index:uint ):void
		{
			if (index > 0 && index <= length) 
			{
				for (var i:uint = index -1; i < length - 1 ; i++ )
				{
					groups[i] = groups[i + 1];
				}
				groups[length - 1] = null;
				groups.pop();
			}
		}
		
		public function getMaxValue():Number
		{
			return this.maxValue;
		}
		
		public function getMinValue():Number
		{
			return this.minValue;
		}
		
		public function getLength():uint
		{
			return length;
		}
		
		public function getTotalValues():Array
		{
			return totoalValues;
		}
		
		public function toString():String
		{
			var str:String = "Object:GroupSet ";
			str += "[length = " + length + "] \n";
			str += "[maxValue = " + maxValue + "]";
			str += "[minValue = " + minValue + "]";
			for (var i:uint = 1; i <= length; i++ )
			{
				str += getGroup(i);
			}
			str += "\n";
			return str;
		}
	}
	
}